import time
import allure
import pytest
import requests
import os
from .data import load_deviceAdddata,baseurl
basedir=os.path.dirname(__file__)
filepath=os.path.join(basedir,"testCase/deviceAdd.json")
@allure.epic("添加设备接口")
class TestDeviceAdd:
    def setup_class(self):
        self.session=requests.session()
        self.url = f"{baseurl}/devices/add"
        print("添加设备接口测试开始：",time.strftime("%Y-%m-%d-%H:%M:%S"))
    def teardown_class(self):
        self.session.close()
        print("添加设备接口测试结束：",time.strftime("%Y-%m-%d-%H:%M:%S"))
    @allure.story("普通用户绑定设备")
    @pytest.mark.parametrize("testCase,device_id,name,msg,status_code",load_deviceAdddata(filepath))
    @allure.title("{testCase}")
    def test_deviceAdd(self,testCase,device_id,name,msg,status_code):
        self.session.post("http://127.0.0.1:8000/login", data={"username": "user10", "password": "111111"})
        data={
            "device_id":device_id,
            "name":name
        }
        responses=self.session.post(self.url,data=data)
        assert status_code==responses.status_code
        assert msg in responses.url
        self.session.cookies.clear()

    @allure.story("其他用户绑定设备")
    @allure.title("医生添加设备")
    def test_deviceAddAnother1(self):
        self.session.post("http://127.0.0.1:8000/login", data={"username": "doctor", "password": "111111"})
        data = {
            "device_id":"ECG2023000000",
            "name":"20230"
        }
        responses = self.session.post(self.url, data=data)
        assert 200 == responses.status_code
        assert "permission_denied" in responses.url
        self.session.cookies.clear()

    @allure.title("管理员添加设备")
    def test_deviceAddAnother2(self):
        self.session.post("http://127.0.0.1:8000/login", data={"username": "manage", "password": "111111"})
        data = {
            "device_id": "ECG2023000001",
            "name": "20231"
        }
        responses = self.session.post(self.url, data=data)
        assert 200 == responses.status_code
        assert "permission_denied" in responses.url
        self.session.cookies.clear()

    @allure.title("超级管理员添加设备")
    def test_deviceAddAnother3(self):
        self.session.post("http://127.0.0.1:8000/login", data={"username": "root", "password": "111111"})
        data = {
            "device_id": "ECG2023000002",
            "name": "20232"
        }
        responses = self.session.post(self.url, data=data)
        assert 200 == responses.status_code
        assert "permission_denied" in responses.url
        self.session.cookies.clear()